# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=libusb
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=1.0.26
pkgrel=1
pkgdesc="Library that provides generic access to USB devices (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32' 'clangarm64')
url="https://libusb.info/"
license=("spdx:LGPL-2.1")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc" "${MINGW_PACKAGE_PREFIX}-autotools")
options=('staticlibs' 'strip')
source=("https://github.com/libusb/libusb/releases/download/v${pkgver}/${_realname}-${pkgver}.tar.bz2"
        "fix-dll-exports.patch"
        "010-cdecl.patch")
sha256sums=('12ce7a61fc9854d1d2a1ffe095f7b5fac19ddba095c259e6067a46500381b5a5'
            'e25a317610623df4c020ff904661f52b1de6c9f95f3966aef52cf34886c768b3'
            '89f47f73705c231eb766ae2ba7357c7569b8b6ff5881e274a4c0ed02c877833b')

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"

  if [[ ${MINGW_PACKAGE_PREFIX} == *-clang-* ]]; then
    sed -e "s/-mwin32//g" -e "s/-Wl,--add-stdcall-alias//g" -i configure.ac
  fi

  patch -Nbp1 -i "${srcdir}/fix-dll-exports.patch"
  patch -Nbp1 -i "${srcdir}/010-cdecl.patch"

  autoreconf -fiv
}

build() {
  [[ -d "${srcdir}/build-${MSYSTEM}" ]] && rm -rf "${srcdir}/build-${MSYSTEM}"
  mkdir -p "${srcdir}/build-${MSYSTEM}" && cd "${srcdir}/build-${MSYSTEM}"

  ../${_realname}-${pkgver}/configure \
    --prefix=${MINGW_PREFIX} \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --enable-shared \
    --enable-static

  make
}

check() {
  cd "${srcdir}/build-${MSYSTEM}"
  make check || true
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"
  make DESTDIR="${pkgdir}" install
  install -Dm644 "${srcdir}/${_realname}-${pkgver}/COPYING" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING"
}
